package com.cap.bts.crmdp.dealer.meal.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.meal.constants.MealConstants;
import com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao;
import com.cap.bts.crmdp.dealer.meal.domain.MealModel;
import com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto;
import com.cap.bts.framework.common.dao.callback.NativeQueryJpaCallback;
import com.cap.bts.framework.common.dao.callback.NativeQueryWithPagingJpaCallbackForJavaDomain;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Pager;

/**
 * 套餐信息数据处理
 * 
 * @author liZhiYong
 * @version $Id: MealManageDaoImpl.java, v 0.1 2015-9-14 下午4:21:22 1111 Exp $
 */
@Repository("iMealManageDao")
public class MealManageDaoImpl extends BaseDaoImpl<MealModel, Long> implements IMealManageDao {

    @Inject
    public MealManageDaoImpl(@Value("com.cap.bts.crmdp.dealer.meal.domain.MealModel") Class<MealModel> clazz) {
        super(clazz);

    }

    /**
     * 查询套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#getMealList(com.cap.bts.crmdp.dealer.meal.domain.MealModel)
     */
    @Override
    public List<MealModel> getMealList(MealDefineDto dto) throws AppException {
        String selectStr = "select * from tm_crm_package_definition";
        String countStr = "select count(*) from tm_crm_package_definition";
        // 查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();
        // 查询结果集的记录数的SQL
        StringBuilder countSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(" where 1=1 ");
        if (dto.getCode() != null && !"".equals(dto.getCode())) {
            where.append(" and code like '%" + dto.getCode() + "%'");
        }
        if (dto.getName() != null && !"".equals(dto.getName())) {
            where.append(" and name like '%" + dto.getName() + "%'");
        }
        if (dto.getStatus() != null
            && Byte.valueOf("99").longValue() != dto.getStatus().longValue()) {
            where.append(" and status='" + dto.getStatus() + "'");
        }
        String order = " order by id desc";

        selectSql.append(selectStr).append(where.toString()).append(order);
        countSql.append(countStr).append(where.toString());

        Pager page = dto.getPager();
        page.setPageSize(10);

        List<MealModel> list = super.getJpaTemplate().execute(
            new NativeQueryWithPagingJpaCallbackForJavaDomain<MealModel>(selectSql.toString(),
                countSql.toString(), page, MealModel.class));
        dto.setPager(page);
        return list;
    }

    /**
     * 新增套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#insertMealInfo(com.cap.bts.crmdp.dealer.meal.domain.MealModel)
     */
    @Override
    public MealModel insertMealInfo(MealModel mealModel) throws AppException {
        return save(mealModel);
    }

    /**
     * 根据套餐编号查询套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#getMealInfoByCode(com.cap.bts.crmdp.dealer.meal.domain.MealModel)
     */
    @Override
    public MealModel getMealInfoByCode(MealModel mealModel) throws AppException {
        String sqlString = "select * from tm_crm_package_definition where code=?1";
        MealModel mealModel2 = null;

        List<MealModel> mealModels = super.getListByNativeQueryWithVaParam(sqlString,
            mealModel.getCode());
        if (!mealModels.isEmpty()) {
            mealModel2 = mealModels.get(0);
        }
        return mealModel2;
    }

    /**
     * 根据套餐ID修改套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#updateMealInfo(java.lang.Long)
     */
    @Override
    public boolean updateMealInfo(MealDefineDto dto) throws AppException {
        MealModel mealModel = findById(dto.getId());

        mealModel.setName(dto.getName());
        mealModel.setDescription(dto.getDescription());
        mealModel.setStatus(dto.getStatus());
        mealModel.setPrice(dto.getPrice());
        mealModel.setValidPeriod(dto.getValidPeriod());
        mealModel.setUnitOfPeriod(dto.getUnitOfPeriod());
        mealModel.setIsPrepaymentCard(dto.getIsPrepaymentCard());
        mealModel.setNumberSequence(dto.getNumberSequence());
        mealModel.setCardDefId(dto.getCardDefId());
        mealModel.setIsPhysicalCard(dto.getIsPhysicalCard());
        mealModel.setMemberBenfit(dto.getMemberBenfit());

        update(mealModel);
        return true;
    }

    /**
     * 根据套餐ID查询套餐信息
     * 
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#getMealInfoById(com.cap.bts.crmdp.dealer.meal.domain.MealModel)
     */
    @Override
    public MealModel getMealInfoById(MealModel mealModel) throws AppException {
        String sqlString = "select * from tm_crm_package_definition where id=?1";
        MealModel mealModel2 = null;

        List<MealModel> mealModels = super.getListByNativeQueryWithVaParam(sqlString,
            mealModel.getId());
        if (!mealModels.isEmpty()) {
            mealModel2 = mealModels.get(0);
        }
        return mealModel2;
    }

    /**
     * 根据N个条件查询套餐信息
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#getMealModelListByParams(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public List<MealModel> getMealModelListByParams(MealDefineDto mealDefineDto)
                                                                                throws AppException {
        String selectStr = "select * from tm_crm_package_definition a";
        // 查询结果集的SQL
        StringBuilder selectSql = new StringBuilder();

        StringBuilder where = new StringBuilder();
        where.append(" where 1=1 ");
        if (mealDefineDto.getIsPhysicalCard() != null) {
            where.append(" and a.is_physical_card='" + mealDefineDto.getIsPhysicalCard() + "'");
        }
        String order = " order by a.id desc";

        selectSql.append(selectStr).append(where.toString()).append(order);

        return super.getJpaTemplate().execute(
            new NativeQueryJpaCallback<MealModel>(selectSql.toString(), MealModel.class));

    }

    /**
     * 套餐启用或者停用
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#packageEnabled(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public boolean packageEnabled(MealDefineDto dto) throws AppException {
        MealModel mealModel = findById(dto.getId());
        if (dto.getStatus().equals(MealConstants.MEAL_STATUS_DRAFT)) {
            dto.setStatus(MealConstants.MEAL_STATUS_ACTIVE);
        } else if (dto.getStatus().equals(MealConstants.MEAL_STATUS_ACTIVE)) {
            dto.setStatus(MealConstants.MEAL_STATUS_DISCONTINUED);
        } else if (dto.getStatus().equals(MealConstants.MEAL_STATUS_DISCONTINUED)) {
            dto.setStatus(MealConstants.MEAL_STATUS_ACTIVE);
        }
        mealModel.setStatus(dto.getStatus());

        update(mealModel);
        return true;
    }

    /**
     * 根据优惠券和提货券的ID查询有效状态的套餐
     * @see com.cap.bts.crmdp.dealer.meal.dao.IMealManageDao#getMealModelListByTicketId(com.cap.bts.crmdp.dealer.meal.dto.MealDefineDto)
     */
    @Override
    public List<MealModel> getMealModelListByTicketId(Long ticketDefId) throws AppException {
        String sqlString = "SELECT * FROM tm_crm_package_definition tcpd where "
                           + " tcpd.ID IN (SELECT trprl.PACKAGE_DEF_ID FROM tm_raw_package_redemption_lines trprl "
                           + " WHERE trprl.TICKET_DEF_ID =?1) AND tcpd.STATUS ='1'";

        return super.getListByNativeQueryWithVaParam(sqlString, ticketDefId);
    }
}
